/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


Ext.onReady(function(){
    var relate_demension_grid;
    var xg = Ext.grid;  
    var psize = 15;
    var store;
    var openUrl;

    var jReader1 = new Ext.data.JsonReader( {
        root : 'data',
        totalProperty : 'totalCount',
        fields : [{
            name : 'repid'
        }, {
            name : 'repcode'
        },{
            name : 'repname'
        }, {
            name : 'cataname'
        },{
            name:'pointname'
        },{
            name:'pointid'
        },{
            name:'refid'
        }]
    });
    openUrl=basePath + 'JarpListener?className=cn.com.jsepc.rep.process.GetPointRep';
    openUrl += '&methodName=GetAll&paramCount=2';
	
    store = new Ext.data.Store( {
        //storeId : 'store1',
        proxy : new Ext.data.HttpProxy( {
            url : openUrl
        }),
        reader : jReader1,
        baseParams : {
            param_0 : 0,
            param_1 : 15
        }
    });
    

    var sm = new xg.CheckboxSelectionModel({
        });
	
    var cm = new xg.ColumnModel( [
        sm,							
        {
            header : '指标编号',
            sortable : true,
            hidden : true,
            dataIndex : 'repid'
        },{
            header : '指标名称',
            sortable : true,
            dataIndex : 'repname'
        },{
            header : '指标编码',
            sortable : true,
            dataIndex : 'repcode'
        }, {
            header : '所属分类',
            sortable : true,
            dataIndex : 'cataname'
        }, {
            header : '所属关注点',
            sortable : true,
            dataIndex : 'pointname'
        }, {
            header : '所属关注点ID',
            sortable : true,
            hidden : true,
            dataIndex : 'pointid'
        }, {
            header : 'refid',
            sortable : true,
            hidden : true,
            dataIndex : 'refid'
        }
        ]);
	     
    cm.defaultSortable = true;
    
    relate_demension_grid = new Ext.grid.GridPanel({
        // renderTo:'panelDiv',
        store : store,
        title:'全部流程域',
        anchor: '100% -80',
        id:'relate_demension_grid',
        viewConfig : {
            forceFit : true
        },
        sm : sm,
        cm : cm,
        frame : true,
        columnLines : true,
        region:"center",
        height:screen.height-340,
        stateful : true,
        stateId : 'relate_demension_grid',
        bbar : new Ext.PagingToolbar( {
            store : store,
            paramNames : {
                start : 'param_0',
                limit : 'param_1'
            },
            emptyMsg : '没有数据需要显示',
            displayInfo : true,
            pageSize : 15
            
        })
    }); 

    store.load();
    
    var form_panl=new Ext.FormPanel(
    {
        id : 'pl_query',
        anchor: '-0,-0',
        layout : 'form',
        labelWidth : 80,
        //  renderTo:'fieldset_div',
        width : '98%',
        height : '288',
        bodyStyle : 'padding: 0 10px 0 10px',
        border : false,
        items : [ {
            title : '查询条件',
            layout : 'form',
            xtype : 'fieldset',
            bodyStyle : 'padding: 0 0 0 50px',
            collapsible : true,
            collapsed : false,
            labelAlign : 'right',
            height : '100%',
            defaults : {
                width : '100%'
            },
            items : [
            {
                layout : 'column',
                border : false,
                items : [
                {
                    columnWidth : .3,
                    layout : 'form',
                    labelWidth : 80,
                    border : false,
                    bodyStyle : 'padding:5px 5px 0',
                    items : [{
                        xtype:'hidden',
                        id:'relate_tree_id'
                    },{
                        xtype:'hidden',
                        id:'relate_tree_name'
                    },{
                        xtype:'hidden',
                        id:'relate_tree_deep'
                    },
                    {
                        xtype : "textfield",
                        fieldLabel : "编码",
                        anchor : '100%',
                        emptyText : '请选择',
                        id : 'relate_reportcode'
                    } 
                    ]
                },
                {
                    columnWidth : .3,
                    layout : 'form',
                    labelWidth : 80,
                    anchor : '80%',
                    width : '80%',
                    border : false,
                    bodyStyle : 'padding:5px 5px 0',
                    items : [ {
                        xtype : "textfield",
                        fieldLabel : "名称",
                        anchor : '100%',
                        emptyText : '请选择',
                        id : 'relate_reportname'
                    } ]
                },
                {
                    columnWidth : .3,
                    layout : 'form',
                    labelWidth : 80,
                    anchor : '100%',
                    border : false,
                    bodyStyle : 'padding:5px 0 5px 30px',
                    items : [ {
                        xtype : "button",
                        id : 'query_tbtn',
                        text : "查    询",
                        width : 80,
                        handler : function() {
                                        
                            var iExit=Ext.getCmp("relate_tree_id").getValue();
                            if(iExit==""){
                                query_all();  //查询所有
                            }
                            else{
                                query();      //查询单个
                            }
                        }
                    } ]
                } ]
            } ]
        } ]

    });
    
     var tree_root = new Ext.tree.AsyncTreeNode({
        text : '全部流程域',
        id : '0000'
    });
    var tree = new Ext.tree.TreePanel({
       
        
        border:'0',
        //height: 600,
        id:'process_rep_relate_tree',
        anchor:'100% 100%',
        useArrows:true,     //使用箭头
        autoScroll:true,    //自动滚动
        animate:true,       // 动画
        enableDD:false,      //可拖动
        containerScroll: true,  
        rootVisible: true,
        root : tree_root,
        autoScroll : true,        
        listeners: {
            beforeload: function(node) {
            if(node.getDepth()==0){
            
          var openUrl = basePath
                + 'JarpListener?className=cn.com.jsepc.rep.process.GetTree&methodName=gettree&paramCount=0'
                node.loader = new Ext.tree.TreeLoader({
                    dataUrl : openUrl
                });
                }
                else if(node.getDepth()==1){
            var openUrl = basePath
                + 'JarpListener?className=cn.com.jsepc.rep.process.GetTree&methodName=getProcess&paramCount=1&param_0='+node.id
                node.loader = new Ext.tree.TreeLoader({
                    dataUrl : openUrl
                });
                
            }
            else if(node.getDepth()==2){
            var openUrl = basePath
                + 'JarpListener?className=cn.com.jsepc.rep.process.GetTree&methodName=getLifecycle&paramCount=1&param_0='+node.id
                node.loader = new Ext.tree.TreeLoader({
                    dataUrl : openUrl
                });
                
            }
            else if(node.getDepth()==3){
            var openUrl = basePath
                + 'JarpListener?className=cn.com.jsepc.rep.process.GetTree&methodName=getList&paramCount=1&param_0='+node.id
                node.loader = new Ext.tree.TreeLoader({
                    dataUrl : openUrl
                });
                
            }
            else if(node.getDepth()==4){
            var openUrl = basePath
                + 'JarpListener?className=cn.com.jsepc.rep.process.GetTree&methodName=getPoint&paramCount=1&param_0='+node.id
                node.loader = new Ext.tree.TreeLoader({
                    dataUrl : openUrl
                });
                
            }
            }
        }   
    });
    tree.on('click', function(node){
        Ext.getCmp("relate_tree_id").setValue("");
        Ext.getCmp("relate_tree_deep").setValue("");
        Ext.getCmp("relate_reportcode").setValue("");
        Ext.getCmp("relate_reportname").setValue("");
        var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.process.GetPointRep';
        var deep=node.getDepth();
        if(deep==5){
            Ext.getCmp("add_button").setDisabled(false);
             Ext.getCmp("delete_catalog_button").setDisabled(false);
        }
        else{
            Ext.getCmp("add_button").setDisabled(true);
            Ext.getCmp("delete_catalog_button").setDisabled(true);
        }
        if(deep==0){
            put_process_rep();
            Ext.getCmp("relate_demension_grid").setTitle("所有流程域")
            return ;
        }
        if(deep==1){
            Ext.getCmp("relate_demension_grid").setTitle(node.text)
            openUrl += '&methodName=FromDomain&paramCount=3';
  
        }
        else if(deep==2){
            Ext.getCmp("relate_demension_grid").setTitle(node.text)
            openUrl += '&methodName=FromProcess&paramCount=3';

        }
        else if(deep==3){
            Ext.getCmp("relate_demension_grid").setTitle(node.text)
            openUrl += '&methodName=FromLife&paramCount=3';
        }
        else if(deep==4){
            Ext.getCmp("relate_demension_grid").setTitle(node.text)
            openUrl += '&methodName=FromLink&paramCount=3';
        }
        else if(deep==5){
            Ext.getCmp("relate_demension_grid").setTitle(node.text)
            openUrl += '&methodName=FromPoint&paramCount=3';
        }
        Ext.getCmp("relate_tree_id").setValue(node.id);
        Ext.getCmp("relate_tree_deep").setValue(node.getDepth());
        Ext.getCmp("relate_tree_name").setValue(node.text);
        store.proxy = new Ext.data.HttpProxy({
            url:openUrl
        });
        store.baseParams = {
            param_0 : 0,
            param_1 : psize,
            param_2:node.id
        }
        relate_demension_grid.reconfigure(store,cm);
        store.load();
    });
    
    var viewport = new Ext.Viewport({
        layout : 'border',
        items:[{
            tbar : [{
                xtype:'button',
                text : '新增',
                 disabled :true,
                id:'add_button',
                handler : function() {
                    var treeid=Ext.getCmp("relate_tree_id").getValue();
                    var deep=Ext.getCmp("relate_tree_deep").getValue();
                    if(deep!=5){
                        Ext.MessageBox.alert("提示","只有关注点才能关联指标")
                        return ;
                    }
                    if(treeid==""){
                        Ext.MessageBox.alert("提醒","没有选择任何流程！");
                        return ;
                    }
                    window.showModalDialog("subject_report.jsp?deep="+Ext.getCmp("relate_tree_deep").getValue()+"&id="+Ext.getCmp("relate_tree_id").getValue(), '','dialogWidth=900px;dialogHeight=550px;status:no;resizable:yes;');
                    Ext.getCmp("relate_demension_grid").store.load();
                }
            }, '-', {
                xtype:'button',
                text : '删除',
                disabled :true,
                id:'delete_catalog_button',
                handler : function() {
                    var row = relate_demension_grid.getSelectionModel().getSelections(); 
                    if(row.length==0){
                        Ext.MessageBox.alert("提示","请勾选需要删除的指标！")
                        return ;
                    }
                    else{
                        Ext.MessageBox.show({
                            title:'提示',
                            msg: '是否确定删除？',
                            buttons:{
                                'yes':'删除',
                                'no':'取消'
                            },
                            fn: save_or_no,
                            animEl: 'delete_process_button',
                            icon: Ext.MessageBox.QUESTION
                        });
                    }
                    function save_or_no(btn){
                        if(btn=='yes'){
                            var array=new Array(row.length);
                            for(var i=0;i<row.length;i++){
                                array[i]=row[i].get('refid'); 
                            }
                            var saveItemInfoUrl = "";
                            saveItemInfoUrl = '?className=cn.com.jsepc.rep.process.DeletePointRep';
                            saveItemInfoUrl += '&methodName=DeletePointRep';
                            saveItemInfoUrl += '&paramCount=1'+'&param_0='+ array;

                            saveItemInfoUrl = basePath + 'JarpListener' + saveItemInfoUrl;
                            httpR_itemInfo = getXMLHttpRequest();
                            httpR_itemInfo.onreadystatechange = returnItemUpdate;
                            httpR_itemInfo.open("POST", saveItemInfoUrl, true);
                            httpR_itemInfo.send();
                            function returnItemUpdate() {
                                if (httpR_itemInfo.readyState == 4) {
                                    if (httpR_itemInfo.status == 200) { 
                                        ret = httpR_itemInfo.responseText;
                                        if (ret=='ok'){
                                            Ext.Msg.alert('提示','删除成功！');
                                            var grid = Ext.ComponentMgr.get("relate_demension_grid");
                                            grid.store.reload();
                                        }else{
                                            Ext.Msg.alert('提示','删除失败！');
                                        }
                                    }
                                }
                            }
                        }
                        else if(btn=='no'){
                            return ;
                        }
                    };
                    
                }
            }
            ],
            region : 'center',
            deferredRender : false,
            activeTab : 0,
            layout : 'border',
            items:[{
                tools : [{
                    id : 'refresh',
                    handler : function() {
                        if(tree_root!=null){
                            tree_root.reload();
                        }
		
                    }
                }],   
                title: '流程',
                collapsible : true,   
                split:true,
                layout:'anchor',
                autoScroll:true,
                region : 'west',
                width : 200,
                items:[tree]
               // html : '<div id="process_rep_relate_tree_div"></div>'
            }, {
                region : 'center',
                title:'关联的指标',
                deferredRender : false,
                activeTab : 0,
                layout:"anchor",
                frame : true,
                items:[form_panl,relate_demension_grid]
            //   html : "<div id='panelDiv'></div>"
            //     items : [ panel_query ]
            }
            ]
        }]

    })
    
    function put_process_rep(){                       //首次进入默认打开所有指标
        openUrl=basePath + 'JarpListener?className=cn.com.jsepc.rep.process.GetPointRep';
        openUrl += '&methodName=GetAll&paramCount=2';
 
        store.proxy = new Ext.data.HttpProxy({
            url:openUrl
        });
        store.baseParams = {
            param_0 : 0,
            param_1 : psize
        }
        relate_demension_grid.reconfigure(store,cm);  
        store.load();
    }
    
   
        function query(){
        var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.process.PointRepQuery';
        var deep=Ext.getCmp("relate_tree_deep").getValue();
        if(deep==1){
            openUrl += '&methodName=FromDomain&paramCount=5';
        }
        else if(deep==2){
            openUrl += '&methodName=FromProcess&paramCount=5';
        }
        else if(deep==3){
            openUrl += '&methodName=FromLife&paramCount=5';
        }
        else if(deep==4){
            openUrl += '&methodName=FromLink&paramCount=5';
        }
        else if(deep==5){
            openUrl += '&methodName=FromPoint&paramCount=5';
        }
        
        store.proxy = new Ext.data.HttpProxy({
            url:openUrl
        });
        store.baseParams = {
            param_0 : 0,
            param_1 : psize,
            param_2: Ext.getCmp("relate_tree_id").getValue(),
            param_3 : Ext.getCmp("relate_reportcode").getValue(),
            param_4:Ext.getCmp("relate_reportname").getValue()
        }
        relate_demension_grid.reconfigure(store,cm);
        store.load();
    }
    
     function query_all(){
        var openUrl = basePath + 'JarpListener?className=cn.com.jsepc.rep.process.PointRepQuery';
        openUrl += '&methodName=GetAll&paramCount=4';
        store.proxy = new Ext.data.HttpProxy({
            url:openUrl
        });
        store.baseParams = {
            param_0 : 0,
            param_1 : psize,
            param_2 : Ext.getCmp("relate_reportcode").getValue(),
            param_3:Ext.getCmp("relate_reportname").getValue()
        }
        relate_demension_grid.reconfigure(store,cm);
        store.load();
    }
 
})